package com.nondo.rdp.finance.endpoint.api.admin;

import com.nondo.rdp.auth.common.UserUtils;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.auth.endpoint.model.ConvertCall;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.CommUtil;
import com.nondo.rdp.core.common.ValidateUtil;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.analysis.Analysis;
import com.nondo.rdp.finance.model.analysis.AdminAnalysisBean;
import com.nondo.rdp.finance.model.analysis.AnalysisBean;
import com.nondo.rdp.finance.service.analysis.AnalysisCommentService;
import com.nondo.rdp.finance.service.analysis.AnalysisCommentUpService;
import com.nondo.rdp.finance.service.analysis.AnalysisService;
import com.nondo.rdp.finance.service.analysis.AnalysisUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController("apiAdminAnalysis")
@RequestMapping("/api/admin/analysis")
public class AnalysisEndpoint {
    @Autowired
    AnalysisService analysisService;

    @Autowired
    AnalysisCommentService analysisCommentService;

    @Autowired
    AnalysisUpService analysisUpService;

    @Autowired
    AnalysisCommentUpService analysisCommentUpService;

    /**
     * 获取分析详情
     *
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping("/info/{id}")
    public View info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
        View view = new View(20000, "获取数据成功");

        Analysis analysis = null;
        if (id != null) {
            analysis = analysisService.findOne(id);
        } else {
            analysis = new Analysis();
        }

        //数据处理
        AnalysisBean analysisBean = new AnalysisBean();
        BeanUtils.copy(analysis, analysisBean);

        view.setData(analysisBean);
        return view;
    }

    /**
     * 获取分析列表
     *
     * @param request
     * @param response
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public View<Page<AdminAnalysisBean>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Analysis> query) {
        View<Page<AdminAnalysisBean>> view = new View(20000, "获取列表成功");

        Analysis analysis = query.getData();

        Page<Analysis> page = analysisService.findAll(new Page<Analysis>(query.getPage(), query.getSize()), analysis);

        Page<AdminAnalysisBean> resultPage = page.convertPage(new ConvertCall<Analysis, Page<AdminAnalysisBean>>() {
            @Override
            public Page<AdminAnalysisBean> execute(Page<Analysis> srcPage) {
                Page<AdminAnalysisBean> analysisBeanPage = new Page<AdminAnalysisBean>(srcPage.getPageNumber(), srcPage.getPageSize(), srcPage.getTotalPages(), srcPage.getTotalElements());

                //处理数据
                List<AdminAnalysisBean> list = new ArrayList<AdminAnalysisBean>();
                for (Object item : srcPage.getContent()) {
                    AdminAnalysisBean AdminanalysisBean = new AdminAnalysisBean();
                    BeanUtils.copy(item, AdminanalysisBean);

                    AdminanalysisBean.setMainImg(CommUtil.getFileUrl(AdminanalysisBean.getMainImg()));

                    list.add(AdminanalysisBean);
                }
                analysisBeanPage.setContent(list);

                return analysisBeanPage;
            }
        });

        view.setData(resultPage);

        return view;
    }

    /**
     * 添加分析
     *
     * @param request
     * @param response
     * @param analysisBean
     * @param result
     * @return
     */
    @RequestMapping(value = "/add", produces = "application/json;charset=UTF-8")
    public View<AnalysisBean> add(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody AnalysisBean analysisBean, BindingResult result) {
        View<AnalysisBean> view = new View<>(20000, "添加成功");

        User user = UserUtils.getUser(request, response);

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Analysis analysis = new Analysis();
            BeanUtils.copy(analysisBean, analysis);
            analysis.setUser(user);

            //附加功能：新增分析插入随机阅读数量
            analysis.setReadNum((long) CommUtil.getRandNum());

            this.analysisService.save(analysis);
            view.setData(analysisBean);
        }
        return view;
    }

    /**
     * 更新分析
     *
     * @param request
     * @param response
     * @param analysis
     * @param result
     * @return
     */
    @RequestMapping("/update")
    public View<AnalysisBean> update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody AnalysisBean analysis, BindingResult result) {
        View<AnalysisBean> view = new View<>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Analysis analysisDb = this.analysisService.findOne(analysis.getId());
            BeanUtils.copy(analysis, analysisDb);
            this.analysisService.update(analysisDb);
        }

        return view;
    }

    /**
     * 删除分析
     *
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
        View view = new View(20000, "删除成功");

        this.analysisService.delete(id);

        return view;
    }

    /**
     * 获取分析类型列表
     *
     * @return
     */
    @RequestMapping("/typelist")
    public View typeList() {
        View view = new View(20000, "获取成功成功");

        Map map = new HashMap();
        map.put(1, "大咖论市");
        map.put(2, "股民选股");
        map.put(3, "投资策略");

        view.setData(map);

        return view;
    }
}